package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.application.annotation.DataScope;
import com.wlyuan.open.scanner.dao.entity.PriceDocIndexDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-11-07
 */
public interface PriceDocIndexMapper extends BaseMapper<PriceDocIndexDO> {

    String QUOTATION_SQL = "SELECT DISTINCT a.Index_OrderID FROM Price_DocIndex a ";

    String QUERY_SQL = "SELECT a.Index_ID, a.Index_GUID, a.opt_status, a.Index_Code, a.Index_PactCode, ISNULL(a.Index_Name,'') as indexName, a.Index_StartTime, a.Index_EndTime, SUBSTRING(a.Index_Description,0,200) AS indexDescription, a.Index_OrderID," +
            " a.Index_SrcDocID, a.Index_Type, a.Index_Status, a.Index_StatusTime, case a.Index_CustomerID when 0 then null else a.Index_CustomerID end as indexCustomerid, " +
            "a.Index_CustomerCompanyID, a.Index_CreatorID, a.Index_CreatorCompanyID, a.Index_CreateTime, a.Index_Confirmer, a.Index_ConfirmTime, a.Index_Invalid, a.Index_Comments," +
            "case a.Index_SupplierSymbolID when 0 then null else a.Index_SupplierSymbolID end as indexSuppliersymbolid, a.Index_PriceOffer, a.Index_ContractType, a.Index_ExpiredStatus," +
            " a.Index_FromType, case a.Index_SupplierID when 0 then null else a.Index_SupplierID end as indexSupplierid, a.Index_SupplierCompanyID, a.Index_Driver, a.Index_QuoteType," +
            " a.Index_MobilePrice, a.Index_Version, a.Price_CustomerName, a.Price_SupplierName, " +
            "a.Index_mobileMark, a.Index_ContractFromType " +
            "FROM Price_DocIndex a ";

    String TEMPLATE_WHERE = "WHERE a.Index_Type = 2 and a.Index_Invalid = 0 and a.Index_Status in (1,2) and a.Index_EndTime >= '2023-01-01' ";

    String QUOTATION_WHERE = "WHERE a.Index_Invalid = 0 and (a.Index_Type = 1 OR (a.Index_Type = 3 and (Index_Status != 0 or Index_Status = 3)) OR a.Index_Type = 4 OR a.Index_Type = 5) ";

    /**
     * 获取所有报价订单
     *
     * @return
     */
    @Select(QUOTATION_SQL + QUOTATION_WHERE)
    @DataScope(column = "Index_CreatorCompanyID", alias = "a")
    List<Long> getOrderId();

    /**
     * 报价列表
     *
     * @return
     */
    @Select(QUERY_SQL + QUOTATION_WHERE)
    @DataScope(column = "Index_CreatorCompanyID", alias = "a")
    List<PriceDocIndexDO> list();

    /**
     * 合约列表
     *
     * @return
     */
    @Select("SELECT a.Index_ID FROM Price_DocIndex a  " + TEMPLATE_WHERE + " order by a.Index_ID OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    @DataScope(column = "Index_CreatorCompanyID", alias = "a")
    List<Long> temlpateIds(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);

    @Select(QUERY_SQL + " where a.Index_ID = #{templateId}")
    PriceDocIndexDO getById(Long templateId);

    @Select("SELECT count(*) FROM Price_DocIndex a " + TEMPLATE_WHERE)
    @DataScope(column = "Index_CreatorCompanyID", alias = "a")
    Integer count();

    @Select("SELECT count(*) FROM Price_DocIndex a " + TEMPLATE_WHERE + " and a.Index_CreatorCompanyID = #{tenantId}")
    @DataScope(column = "Index_CreatorCompanyID", alias = "a")
    Integer countByTenantId(@Param("tenantId") Long tenantId);

    @Select("SELECT a.Index_ID FROM Price_DocIndex a  " + TEMPLATE_WHERE + "  and a.Index_CreatorCompanyID = #{tenantId} order by a.Index_ID OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    @DataScope(column = "Index_CreatorCompanyID", alias = "a")
    List<Long> temlpateIdByTenantId(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("tenantId") Long tenantId);

    /**
     * 根据订单id获取报价
     *
     * @param orderId
     * @return
     */
    @Select(QUERY_SQL + " WHERE  a.Index_Invalid = 0 and a.Index_Type in (1,3) and a.Index_Status IN (0,1,2,3,8) AND a.Index_OrderID = #{orderId} order by a.Index_Type asc, a.Index_ID desc")
    List<PriceDocIndexDO> getWaybillQuotationByOrderId(Long orderId);

    @Select(QUERY_SQL + " WHERE  a.Index_Invalid = 0 and a.Index_Type in (4,5) and a.Index_Status IN (0,1,2,3,8) AND a.Index_OrderID = #{orderId}")
    List<PriceDocIndexDO> getCombineQuotationByOrderId(Long orderId);

    @Select(QUERY_SQL + " WHERE a.Index_OrderID = #{orderId} and a.Index_Status = 2 ")
    List<PriceDocIndexDO> getCheckByOrderId(Long orderId);

    @Select("SELECT a.Index_ID FROM Price_DocIndex a  " + TEMPLATE_WHERE + " and a.Index_CreateTime >= #{startTime} and a.Index_CreateTime <= #{endTime} ")
    List<Long> getByTime(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT a.Index_ID FROM Price_DocIndex a  " + TEMPLATE_WHERE + " and a.Index_CreatorCompanyID = #{tenantId} and a.Index_CreateTime >= #{startTime} and a.Index_CreateTime <= #{endTime} ")
    List<Long> getByTenantIdTime(@Param("tenantId") Long tenantId, @Param("startTime") String startTime, @Param("endTime") String endTime);


    @Select(QUERY_SQL + TEMPLATE_WHERE + " and a.Index_ID = #{templateId}")
    PriceDocIndexDO getTemplateByTemplate(Long templateId);
}
